package com.app.service.impl;

import com.app.common.Result;
import com.app.mapper.TbcBankMapper;
import com.app.model.Bank;
import com.app.mapper.BankMapper;
import com.app.model.TbcBank;
import com.app.model.UserModel;
import com.app.service.IBankService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 银行卡表 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2024-09-03
 */
@Service
public class BankServiceImpl extends ServiceImpl<BankMapper, Bank> implements IBankService {

    @Autowired
    private BankMapper bankMapper;
    @Autowired
    private TbcBankMapper tbcBankMapper;

    @Override
    public Result addBank(Bank bank, UserModel userModel) {

        Bank b = bankMapper.selectOne(new QueryWrapper<Bank>().eq("bank_num", bank.getBankNum()));
        if (b != null) {
            return Result.error(400, "该账户已存在");
        }

        bank.setUserId(userModel.getId());
        bank.setCreateTime(LocalDateTime.now());
        bankMapper.insert(bank);
        return Result.success(bank);
    }

    @Override
    public IPage getBankList(Integer page, Integer limit, Integer userId) {
//        List<Bank> banks = bankMapper.selectList(new QueryWrapper<Bank>().eq("user_id", userId).orderByDesc("create_time"));
//        List<TbcBank> tbcBanks = tbcBankMapper.selectList(new QueryWrapper<>());
//        Map<Integer, String> collect = tbcBanks.stream().collect(Collectors.toMap(TbcBank::getId, TbcBank::getBankName));
//        banks.stream().map(c->{
//            if(c.getBankName()!=null){
//                c.setBankName(collect.getOrDefault(Integer.parseInt(c.getBankName()),c.getBankName()));
//            }
//            return c;
//        }).forEach(a->{});
        Page p = new Page(page, limit);
        IPage<Bank> bankIPage = this.bankMapper.getBankList(p, userId);
        return bankIPage;
    }
}
